<?php
/**
 * @title 376. 摆动序列 - 动态规划
 * @author start2004
 */

class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer
     */
    function wiggleMaxLength($nums) {
        /**
         * @since 2020-12-14 长度
         */
        $len = count($nums);
        if($len == 0){
            return 0;
        } else {}

        /**
         * @since 2020-12-14 上个摆动的方向
         */
        $direction = 0;

        /**
         * @since 2020-12-14 结果集
         */
        $result = 1;

        /**
         * @since 2020-12-14 上个摆动的最大值或者最小值, 即波峰波谷
         */
        $minMax = $nums[0];

        /**
         * @since 2020-12-14 迭代, 动态规划
         */
        for($i=1; $i<$len; $i++){
            $num = $nums[$i];
            if($num == $minMax){
                continue;
            } elseif($num > $minMax) {
                /**
                 * @since 2020-12-14 正数
                 */
                if($direction <= 0){
                    $result++;
                    $minMax = $num;
                    $direction = 1;
                } else {
                    /**
                     * @since 2020-12-14 上坡, 取更大值, 易于下坡
                     */
                    $minMax = $num;
                }
            } else {
                /**
                 * @since 2020-12-14 负数
                 */
                if($direction >= 0){
                    $result++;
                    $minMax = $num;
                    $direction = -1;
                } else {
                    /**
                     * @since 2020-12-14 下坡, 取更小值, 易于上坡
                     */
                    $minMax = $num;
                }
            }
        }

        /**
         * @return
         */
        return $result;
    }
}




/**
 * @url http://127.0.0.1/leetcode/202008/2020.08.03_4.php
 */
$datas = [
    [1,7,4,9,2,5], // 6
    [1,1,1,], // 1
    [1,17,5,10,13,15,10,5,16,8], // 7
    [1,2,3,4,5,6,7,8,9], // 2
    [1,1,1,1,2], // 2
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->wiggleMaxLength($data);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
